<template>
  <el-dialog
    v-loading="loading"
    :visible.sync="dialog"
    title="编辑"
    width="600px"
    :close-on-click-modal="false"
    append-to-body
    @close="cancel"
  >
    <div>
      <el-form ref="suspendForm" :model="param" :rules="rules" size="small" label-width="120px">
        <el-form-item label="暂停原因：" prop="suspendReason">
          <el-input v-model="param.suspendReason" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="暂停时间：" prop="suspendTime">
          <el-date-picker
            v-model="suspendTime"
            type="datetimerange"
            :clearable="false"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="suspendTimeChange"
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button type="primary" @click="doSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Message } from 'element-ui'
import { save } from '@/api/suspend'

export default {
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    var dateTimeVali = (rule, value, callback) => {
      if (!this.param.beginDate || !this.param.endDate) {
        callback(new Error('请选择暂停时间'))
      } else {
        callback()
      }
    }
    return {
      dialog: false,
      loading: false,
      param: {},
      suspendTime: [],
      rules: {
        suspendReason: [
          { required: true, message: '请填写暂停原因', trigger: 'blur' }
        ],
        suspendTime: [
          { validator: dateTimeVali, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.dialog = false
      this.param = {}
    },
    // 日期组件触发
    suspendTimeChange(val) {
      this.param.beginDate = val[0]
      this.param.endDate = val[1]
    },
    // 确定按钮触发
    doSubmit() {
      this.$refs.suspendForm.validate((valid) => {
        if (valid) {
          this.loading = true
          save(this.param).then(res => {
            this.parent.toQuery()
            Message({
              message: '操作成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
            this.dialog = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
